
// Page({
//   onLoad(options) {
//     console.log(options);
//     this.setData({
//       title: options.title,
//     });
//   },
// });

import lifecycle from '/util/lifecycle';
import animModal from '/util/items';

let app = getApp();

let url = app.userUrl + '/DingDing/AccountManagement/bindUserWithDingDing';


Page({
  data: {
    dingdingId: '',
    username: '',
    password: '',
  },

  username(e){
    this.setData({
      username: e.detail.value
    })
  },

  password(e) {
    this.setData({
      password: e.detail.value
    })
  },

  onLoad(){
    dd.getStorage({
      key: 'currentUser',
      success: (res) => {
          this.setData({
            dingdingId: res.data.userId
          })
        }
    })
  },

  submit(){
    console.log("从全局那里得到的dingdingId: ",app.globalData.dingdingId)
    let username = this.data.username
    let password = this.data.password
    let dingdingId = this.data.dingdingId

    if(username == null || username == "" || password == null || password == ""){
      dd.alert({
        content: '用户名密码不能为空',
      })
    }else{
      dd.showLoading({
        content: '登录中'
      })
      dd.httpRequest({
          headers: {
            "Content-Type": "application/json"
          },
          url: url,
          method: 'POST',
          data:  JSON.stringify({
            username: username,
            password: password,
            dingdingId: dingdingId
          }),
          dataType: 'json',
          success: (res) => {
            dd.hideLoading()
            if(res.data.result == "success"){
              dd.httpRequest({
              url: app.baseUrl + '/DingDingSystem2/getInspectInfo',
              method: 'POST',
              headers: {
                "Content-Type": "application/json"
              },
              data: JSON.stringify({
                schoolId: res.data.departmentId,
                userId:  res.data.id,
                identityId:  res.data.identityId,
              }),
              dataType: 'json',
              success: res => {
                dd.setStorageSync({
                  key: "allList",
                  data: res.data
                })
              }
            })
            
              app.globalData.userId = res.data.id
              app.globalData.userName = res.data.username
              app.globalData.department = res.data.department
              app.globalData.departmentId = res.data.departmentId
              app.globalData.inspectModel = res.data.inspectModel
              app.globalData.identity = res.data.identity
              app.globalData.identityId = res.data.identityId
              app.globalData.userType = res.data.userType
              app.globalData.departmentType = res.data.departmentType
              app.globalData.realname = res.data.realName
              app.globalData.phone = res.data.phone
              app.globalData.position = res.data.position
              app.globalData.parent = res.data.parent
              
              //跳转到主页面
              dd.reLaunch({
                url: '/page/index'
              })
            }else{
              dd.alert({
                content: res.data.error + '请重试！'
              })
            }
          },
          fail: (res) => {
            console.log('=============23')
              dd.alert({
                content: '网络异常'
              })
          }
      })

    }
  },

  // onShow(){
  //   dd.getStorage({
  //       key: 'currentUser',
  //       success: (res) => {
  //         // dd.alert({
  //         //   content: '获取成功' + res.data.userId
  //         // })
  //         this.setData({
  //           dingdingId: res.data.userId
  //         })
  //       }
  //   })
  //   //console.log("dingdingId: ",dingdingId);
  // }

})

